PHP Character Functions

    PHP supports some of the character functions that will allow you to manipulate with characters and string. Whenever you call these functions with the integer argument they behave exactly like their C counterparts from ctype.h. Below is the list of the functions that you can consider using with character-

    1. ctype_alnum()

    This function will allow you to check for the alphanumeric characters. Syntax-

    ctype_alnum ( $text );

    Example-

    <?php
       $strings = 'foo!#$bar';
       if (ctype_alnum($strings))
       echo 'string is alnum';
       else
       echo 'not alnum';
       ?>

    Output- not alnum

    2. ctype_alpha()

    This function will allow you to check for the presence of the alphabetic characters. Syntax-

    ctype_alpha ( $text );

    Example-

    <?php
       $strings = 'foo!#$bar';
       if (ctype_alpha($strings))
       echo 'string is alpha';
       else
       echo 'not alpha';
       ?>

    Output-

    not alpha

    3. ctype_cntrl()

    This function will allow you to check for the control characters present in the given string or text. Syntax-

    ctype_cntrl ( $text );

    Example-

    <?php
       $strings = '\n\r\t';
       if (ctype_cntrl($strings))
       echo 'string is control';
       else
       echo 'not control';
       ?>

    Output-

    not control

    4. ctype_digit()

    This function will allow you to check for the presence of the numeric characters within the given text or string. Syntax-

    ctype_digit ( $text );

    Example-

    <?php
       $strings = '20.10';
       if (ctype_digit($strings))
       echo 'string is digit';
       else
       echo 'not digit';
       ?>

    Output-

    not digit

    5. ctype_graph()

    This function will allow you to check for printable characters present except space character. Syntax-

    ctype_graph ( $text );

    Example

    <?php
       $strings = 'asdf\n\r\t';
       if (ctype_graph($strings))
       echo 'string is graph';
       else
       echo 'not graph';
       ?>

    Output-

    string is graph

    6. ctype_lower()

    This function will allow you to check for the presence of the lowercase characters present within the given text. Syntax-

    ctype_lower ( $text );

    Example-

    <?php
       $strings = 'aac123';
       if (ctype_lower($strings))
       echo 'string is lower';
       else
       echo 'not lower';
       ?>

    Output-

    not lower

    7. ctype_print()

    This function will allow you to check for the printable characters. Syntax-

    ctype_print ( $text );

    Example-

    <?php
       $strings = 'k211';
       if (ctype_print($strings))
       echo 'string is print';
       else
       echo 'not print';
       ?>

    Output-

    string is print

    8. ctype_punct()

    This function will allow you to check for the presence of any printable character except the whitespace or alphanumeric character. Syntax-

    ctype_punct ( $text );

    Example-

    <?php
       $strings = '*$()';
       if (ctype_punct($strings))
       echo 'string is punct';
       else
       echo 'not punct';
       ?>

    Output-

    string is punct

    9. ctype_space()

    This function will allow you to check for the whitespace character present in the text. Syntax-

    ctype_space ( $text );

    Example-

    <?php
       $strings = '\test123 12';
       if (ctype_space($strings))
       echo 'string is space';
       else
       echo 'not space';
       ?>

    Output-

    not space

    10. ctype_upper()

    This function will allow you to check for uppercase characters. Syntax-

    ctype_upper ( $text );

    Example-

    <?php
       $strings = 'ABCEFG';
       if (ctype_upper($strings))
       echo 'string is upper';
       else
       echo 'not upper';
       ?>

    Output-

    string is upper

    11. ctype-xdigit()

    This function will check for hexadecimal digit within the given text. Syntax-

    ctype_xdigit ( $text );

    Example-

    <?php
       $strings = 'SAI!@#$';
       if (ctype_xdigit($strings))
       echo 'string is xdigit';
       else
       echo 'not xdigit';
       ?>

    Output-

    not xdigit

    People are also reading: